//
//  UploadImageTask.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/12/26.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay

open class UploadImageTask:AsynJoTask{
    
    public static var defaultUrl:String = "url"
    public static var defaultTargetNode:String = ""
    
    
    public var model:NSMutableDictionary = NSMutableDictionary()
    
    public var sessionTask:URLSessionUploadTask? = nil
    
    public var targetNode:String?
    
    open override func canleTask() {
        sessionTask?.cancel()
    }
    
    open var url:String? = nil
    
    open override func startTask() {
        let item = model
        weak var wself = self
        if item["__url",""].len > 0
        {
            
            wself?.finish(success: true, objs: item)
            
            return
        }
        
        
        let node = self.targetNode ?? UploadImageTask.defaultTargetNode
        
        let file_path = item["path",""]
        if file_path != ""
        {
            
            let url = self.url ?? UploadImageTask.defaultUrl
            if url.len < 1
            {
                fatalError("请配置UploadImageTask 中的defaultUrl 或 UploadImageTask实例的url")
            }
            let mime = MimeFile.filePath(file_path)
            let file = TaskFile(mimeFile: mime)
            JoTask().log.post.log_response().url(url).append(file: file).param_body_encoding.json_handle { (status, message, obj, response) in
                if status.isSuccess
                {
                    
                    if let root = obj as? NSMutableArray{
                        let url = root[node,""]
                        if url.len > 1{
                            item["__url"] = url
                            wself?.finish(success: true, objs: NSMutableDictionary())
                            return
                        }
                    }
                    if let root = obj as? NSMutableDictionary{
                        let url = root[node,""]
                        if url.len > 1{
                            item["__url"] = url
                            wself?.finish(success: true, objs: NSMutableDictionary())
                            return
                        }
                        
                    }
                    if let url = obj as? String
                    {
                        item["__url"] = url
                        wself?.finish(success: true, objs: item)
                        return
                    }
                    
                    
                    
                }
                wself?.finish(success: false, objs: item)
                
                }.easyUpload()
        }
        else{
            let filepath = item["__url",""]
            if filepath.len > 2
            {
                
                wself?.finish(success: true, objs: item)
                return
            }
            else{
                wself?.finish(success: false, objs:NSMutableDictionary())
            }
        }
        
        
    }
}




